import {
  newGetGroupList
} from '../../apis/group';
import {
  globalData
} from '../../utils/request';
const app = getApp();

Page({
  data: {
    groupList: [],
    pageNum: 1,
    isHaveMore: true,
    isRequest: false
  },

  onLoad() {
    this.setData({
      showGroupBtn: globalData.checkIn
    });
  },

  onShow() {
    this.getGroupList(1, 10);
  },

  onReachBottom() {
    if (this.data.isHaveMore) {
      this.setData({
        pageNum: this.data.pageNum + 1
      });
      this.getGroupList(this.data.pageNum, 10);
    };
  },

  getOfficialGroupList(pageNum, pageSize, type) {
    if (this.data.officialRequest) return
    this.setData({
      officialRequest: true
    });
    getGroupList(1, pageNum, pageSize).then(res => {
      this.setData({
        officialRequest: false
      });
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.image) && !new RegExp('http://').test(v.image) && v.image !== '') v.image = app.globalData.url + v.image;
        });
        if (type === 'refresh') {
          this.setData({
            officialList: rows,
            officialTotal: total
          });
        } else {
          rows.forEach(v => {
            this.data.officialList.push(v);
          });
          this.setData({
            officialList: this.data.officialList,
            officialTotal: total
          });
        };
        if (this.data.officialList.length === total) {
          this.setData({
            officialListHaveMore: false
          });
        };
      };
    });
  },
  getUserGroupList(pageNum, pageSize, type) {
    if (this.data.userRequest) return
    this.setData({
      userRequest: true
    });
    getGroupList(2, pageNum, pageSize).then(res => {
      this.setData({
        userRequest: false
      });
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.image) && !new RegExp('http://').test(v.image) && v.image !== '') v.image = app.globalData.url + v.image;
        });
        if (type === 'refresh') {
          this.setData({
            userList: rows,
            userTotal: total
          });
        } else {
          rows.forEach(v => {
            this.data.userList.push(v);
          });
          this.setData({
            userList: this.data.userList,
            userTotal: total
          });
        };
        if (this.data.userList.length === total) {
          this.setData({
            userListHaveMore: false
          });
        };
      };
    });
  },

  getGroupList(pageNum, pageSize) {
    if (this.data.isRequest) return;
    this.setData({
      isRequest: true
    });
    newGetGroupList(pageNum, pageSize).then(res => {
      this.setData({
        isRequest: false
      });
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.image) && !new RegExp('http://').test(v.image) && v.image !== '') v.image = app.globalData.url + v.image;
          this.data.groupList.push(v);
        });
        this.setData({
          groupList: this.data.groupList
        });
        if (this.data.groupList.length === total) {
          this.setData({
            isHaveMore: false
          });
        };
      };
    });
  },

  // 创建群
  createGroup() {
    wx.navigateTo({
      url: '/pages/myCard/index?type=createGroup'
    });
  },

  selectGroup(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.setStorageSync('selectGroup', JSON.stringify({
      groupId: id
    }));
    wx.navigateBack({
      delta: 1
    });
  }
})